package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.utils.Utils;

/**
 * 我参与的活动
 * 
 * @author yejianjun
 *
 */
public class IReleaseActiveAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<ActivityListData> mList;
	
	public IReleaseActiveAdapter(Context context) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(null == mList)
			mList = new ArrayList<ActivityListData>();
	}
//	public void setData(List<ActivityListData> list){
//		mList = list;
//	}
	
	public void clearData(){
		if(null !=  mList){
			mList.clear();
		}
	}
	public void addData(List<ActivityListData> list){
		if(null == list)
			return;
		for (int i = 0; i < list.size(); i++) {
			mList.add(list.get(i));
		}
		
	}
	public List<ActivityListData> getList(){
		return mList;
	}
//	public void addTopData(ActivityListData data){
//		mList.add(0, data);
//	}
//	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GridHolder holder= null;
		if (convertView == null ) {
			convertView = mInflater.inflate(R.layout.irelease_list_item, null);   
			holder = new GridHolder();
			holder.subject = (TextView) convertView.findViewById(R.id.item_subject);
			holder.startDate = (TextView) convertView.findViewById(R.id.item_startDate);
			holder.status = (TextView) convertView.findViewById(R.id.item_status);
			holder.browseCount = (TextView) convertView.findViewById(R.id.item_browseCount);
			holder.inCount = (TextView) convertView.findViewById(R.id.item_inCount);
			holder.passCount = (TextView) convertView.findViewById(R.id.item_passCount);
			convertView.setTag(holder);
		}else{
			holder = (GridHolder) convertView.getTag();
		}
		ActivityListData data = mList.get(position);
		if(null != data){
			holder.subject.setText(data.getSubject()== null ? "": data.getSubject());
			holder.startDate.setText(data.getStartDate()== null ? "": data.getStartDate());
			if(Utils.IsActivityStart(data.getStartDate())){
				holder.status.setText(mContext.getResources().getString(R.string.has_ended));
			}else{
				holder.status.setText(mContext.getResources().getString(R.string.registration));
				holder.status.setTextColor(mContext.getResources().getColor(R.color.blue_text));
			}
			
			holder.browseCount.setText(mContext.getResources().getString(R.string.browse) + data.getBrowseCount());
			holder.inCount.setText(mContext.getResources().getString(R.string.sign_up)  + data.getInCount() );
			holder.passCount.setText(mContext.getResources().getString(R.string.part_in) + data.getPassCount());
		}
		
		return convertView;
	}

	private class GridHolder {
		public TextView subject;
		public TextView startDate;
		public TextView status;
		public TextView browseCount;
		public TextView inCount;
		public TextView passCount;
	}
}
